﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using HZ.MBSM.DBUtility;

public partial class Bidder_TraAccreditPrint : System.Web.UI.Page
{
    public string _strProjectId;
    string UserCode, _strAccountId;
    private string _psrID;
    private string _projectName;
    private string _companyName;
    private int _redcordNum;

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _strProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"].ToString());
                UserCode = _Account.UserCode;
                _strAccountId = _Account.AccountId.ToString();
                _psrID = SysFun.IsSQL(Request.QueryString["Psrid"].ToString());
                DataBind();
                ImportWord();
            }
        }
    }



    /// <summary>
    /// 保存为word文件
    /// </summary>
    private void ImportWord()
    {
        GetProjectName();
        GetCompanyName();
        Response.ContentType = "application/ms-word";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("配送授权声明" + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".doc\"");
        StringBuilder sb = new StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        sb.Append("<html><body margin-top='10%' margin-bottom='10%' margin-left='15%' margin-right='15%'>");
        sb.Append("<div style='width:90%;'><p><center><h3>" + _projectName + "</h3></center>");
        sb.Append("<center><h3>配送授权声明</h3></center></p>");
        sb.Append("</br>");
        sb.Append("</br>");
        sb.Append("<p>致：云南省医药集中招标采购管理服务中心</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;鉴于我方作为“" + _projectName + "”的中标人，履约期间应严格按照《" + _projectName + "文件》和医疗机构的要求，提供优质、全面、快捷的配送服务，对所有中标商品的质量负责。对于中标药品的配送工作将按照《" + _projectName + "文件》第二章第28款相关内容执行。</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;我方现授权<u><b>"+_companyName+"</b></u>（公司名称）作为以下中标药品在云南省内的一级配送企业，该公司可在各省辖市指定1-4家经销商为医疗机构提供中标药品的配送服务，详细药品目录见附表。</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;本声明自盖章之日起生效，被授权人在办理《"+_projectName+"采购配送协议书》时即视为“中标人”，拥有中标人的一切权利与义务。本声明一式叁份，中标人、被授权人、云南省医药集中招标采购管理服务中心各存档一份。</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;（双方可根据需要增加相应条款）</p>");
        sb.Append("<p><table witdh='100%' border='0'>");
        sb.Append("<tr height='40px'><td width='35%'>中标人（盖章）：</td><td width='15%'></td><td width='35%'>被授权人（盖章）：</td><td width='15%'></td></tr>");
        sb.Append("<tr><td >中标人代表（签字）：</td><td ></td><td >被授权人代表（签字）：</td><td ></td></tr>");
        sb.Append("<tr><td >日期：</td><td ></td><td >日期：</td><td ></td></tr>");
        sb.Append("</table></p>");
        sb.Append("<p>附表:</p>");
        sb.Append("<center>");
        this.gvPsInfo.RenderControl(hw);
        sb.Append("</center>");
        sb.Append("<p>以上中标品目共计<b>"+_redcordNum+"</b>个（附表需加盖公章）。<p>");
        sb.Append("</div>");
        sb.Append("</body></html>");
        Response.Write(sb.ToString());
        Response.End(); 
    }

    /// <summary>
    /// 获取项目名称
    /// </summary>
    private void GetProjectName()
    {
        string sql = "select title  from project where projectid=" + _strProjectId;
        Db db = new Db();
        IDataReader dr = db.DataReader(sql);
        if (dr.Read())
        {
            _projectName = dr["title"].ToString();
        }
        dr.Close();
        db.Close();
    }

    /// <summary>
    /// 获取配送企业名称
    /// </summary>
    private void GetCompanyName()
    {
        string sql = "select companyname  from company where accountid=" + _psrID;
        Db db = new Db();
        IDataReader dr = db.DataReader(sql);
        if (dr.Read())
        {
            _companyName = dr["companyname"].ToString();
        }
        dr.Close();
        db.Close();
    }

    /// <summary>
    /// 查询主配送人的配送商品
    /// </summary>
    private void DataBind()
    {
        string priceTitle = "挂网价";
        string fieldName = "";
        string field = "";
        switch (_strProjectId)
        {
            case "8":
                fieldName = "最高零售价";
                field = " ,convert( numeric(20,2), RetailPrice)  as " + fieldName;
                break;
            case "9":
                priceTitle = "最高零售价";
                break;

            case "10":
                priceTitle = "最高限价";
                fieldName = "最高零售价";
                field = " ,convert( numeric(20,2), RetailPrice)  as " + fieldName;
                break;
            case "11":
                break;

        }

        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods";      //要查询的表明
        dbPage.Key = "SupplyGoodsId";     //表的主键    
        dbPage.Where = GetSqlFilter();     //查询条件   

        dbPage.SelectField = "goodsid as 流水号,productname as 药品通用名称,goodsname as 商品名,medicinemodel as 剂型,outlookc as 规格,factor as 转换系数,companyname_sc as 生产厂家,convert( numeric(20,2), bidprice)  as " + priceTitle + field;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 2000;                                                    //没有显示记录条数   
        dbPage.OrderBy = "ProductName,MedicineModel,Outlookc";//排序字段

        DataTable dt = dbPage.DataTable(60);
        this.gvPsInfo.DataSource = dt;
        this.gvPsInfo.DataBind();
        _redcordNum=dt.Rows.Count;
        db.Close();
    }

    /// <summary>
    /// 生成查询语句的条件
    /// </summary>
    /// <returns></returns>
    private string GetSqlFilter()
    {
        StringBuilder sbFilter = new StringBuilder(" accountid_tb=" + _strAccountId + " and companyname_ps<>'' and isuse='1' and accountid_ps<>'0'");
        sbFilter.Append(" and accountid_ps =" + _psrID + " and projectid="+_strProjectId);

        return sbFilter.ToString();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    } 
}
